package com.diancan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.Utils.CustomViewBinder;
import com.custom.MySimpleAdapter;
import com.declare.Declare;
import com.mode.CategoryObj;
import com.mode.SelectedMenuObj;
import com.model.Category;

import android.R.integer;
import android.app.ListActivity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchList extends ListActivity {
	
	String[] menuNameStrings;
	Declare declare;
	AutoCompleteTextView autoCompleteTextView;
	SearchListAdapter simpleAdapter;
	ArrayList<HashMap<String, Object>> hashList;
	ArrayList<SelectedMenuObj> Infos = null;
	Button btnStart;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchlist);
		autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoComplete);
		btnStart=(Button)findViewById(R.id.btn_start);
		menuNameStrings=initArray();
		ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
				   this,
				   android.R.layout.simple_dropdown_item_1line,
				   menuNameStrings);
		autoCompleteTextView.setAdapter(aAdapter);
		btnStart.setOnClickListener(new BtnStartClick());
	}
	public String[] initArray()
	{
		ArrayList<String> menuNameList=new ArrayList<String>();
		declare=(Declare)getApplicationContext();
		for(CategoryObj categoryObj:declare.getMenuListDataObj().getCategoryObjs())
		{
			List<SelectedMenuObj> menus=categoryObj.getSelectedMenuObjs();
			Iterator<SelectedMenuObj> iterator2;
			for(iterator2=menus.iterator();iterator2.hasNext();)
			{
				SelectedMenuObj menuObj=iterator2.next();
				menuNameList.add(menuObj.getName());
			}
		}
		
		String[] arrStrings=new String[menuNameList.size()];
		for(int i=0;i<menuNameList.size();i++)
		{
			arrStrings[i]=menuNameList.get(i);
		}
		return arrStrings;
	}
	public void Search(String str)
	{
		if(hashList==null)
		{
			hashList=new ArrayList<HashMap<String,Object>>();
		}
		hashList.clear();
		int i=0;	
		for(CategoryObj categoryObj:declare.getMenuListDataObj().getCategoryObjs())
		{
			int j=0;
			List<SelectedMenuObj> menus=categoryObj.getSelectedMenuObjs();
			Iterator<SelectedMenuObj> iterator2;
			for(iterator2=menus.iterator();iterator2.hasNext();)
			{
				SelectedMenuObj menu=iterator2.next();
				if(menu.getName().contains(str))
				{
					HashMap<String, Object> map=new HashMap<String, Object>();
					map.put("name", menu.getName());
					map.put("cindex", ""+i);
					map.put("rindex", ""+j);
					hashList.add(map);
				}
				j++;
			}
			i++;
		}
		
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		TextView cTextView=(TextView)v.findViewById(R.id.menuCindex);
		TextView rTextView=(TextView)v.findViewById(R.id.menuRindex);
		String strCindex=cTextView.getText().toString();
		String strRindex=rTextView.getText().toString();
		
		MenuGroup parent = (MenuGroup) getParent();
	    final LinearLayout contain = (LinearLayout) parent.findViewById(R.id.group_Layout);
		contain.removeAllViews();
		Intent in = new Intent(getParent(), MenuFlipPage.class);
		in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		in.putExtra("rindex",strRindex);
		in.putExtra("index",strCindex);
		System.out.println(strCindex);
		System.out.println(strRindex);
		LocalActivityManager manager = parent.getLocalActivityManager();
		String currentId = manager.getCurrentId();
		Window window = manager.startActivity("MenuFlipPage", in);
		
		View view=window.getDecorView();		
		contain.addView(view);
		LayoutParams params=(LayoutParams) view.getLayoutParams();
        params.width=LayoutParams.FILL_PARENT;
        params.height=LayoutParams.FILL_PARENT;
        view.setLayoutParams(params);
        
        Animation sAnimation=AnimationUtils.loadAnimation(SearchList.this, R.anim.open_in);
        view.startAnimation(sAnimation);
		
		super.onListItemClick(l, v, position, id);
	}
	class BtnStartClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String str=autoCompleteTextView.getText().toString();
			Search(str);
			if(simpleAdapter==null)
			{
				simpleAdapter=new SearchListAdapter(SearchList.this, hashList,
						R.layout.search_list_item, new String[] { "name", "cindex","rindex"},
						new int[] { R.id.menuName, R.id.menuCindex,R.id.menuRindex});
				setListAdapter(simpleAdapter);
			}
			else {
				simpleAdapter.setmItemList(hashList);
				simpleAdapter.notifyDataSetChanged();
			}
		}
		
	}
	class SearchListAdapter extends SimpleAdapter implements ListAdapter{
		private ArrayList<HashMap<String, Object>> mItemList;
		public ArrayList<HashMap<String, Object>> getmItemList() {
			return mItemList;
		}
		public void setmItemList(ArrayList<HashMap<String, Object>> hashList) {
			this.mItemList = hashList;
		}
		public SearchListAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
			// TODO Auto-generated constructor stub
			mItemList = (ArrayList<HashMap<String, Object>>) data;
		}
		
	}
}
